﻿using EventMgrLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionRoc.Core;

namespace VisionRoc_Measure
{
    public partial class FormSerialPortTool : Form
    {
        SerialPortTool m_SerialPortTool;
        public FormSerialPortTool(SerialPortTool _SerialPortTool)
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景 
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲  
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            InitializeComponent();
            m_SerialPortTool = _SerialPortTool;
            EventMgr.Ins.GetEvent<SerialPortInfo>().Subscribe(LogHandle, Msg => Msg.SolutionName == "Main");  //接受外部发步的记录日志的事件
        }

        private void LogHandle(SerialPortMessage obj)
        {
            textBox1.Text += obj.Msg + "\r\n";
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormSerialPortTool_Load(object sender, EventArgs e)
        {
            try
            {
                m_SerialPortTool.IniSerialPortInfo(Cmb_Port, Cmb_Baud, Cmb_Parity, Cmb_StopBits, Cmb_DataBits);
                m_SerialPortTool.ReadParameter();
                if (m_SerialPortTool.IsOpenState)
                {
                    Cmb_Port.Text = m_SerialPortTool.Port;
                    Cmb_Baud.Text = m_SerialPortTool.Baud.ToString();
                    Cmb_Parity.Text = m_SerialPortTool.Parity;
                    Cmb_StopBits.Text = m_SerialPortTool.StopBits;
                    Cmb_DataBits.Text = m_SerialPortTool.DataBits.ToString();
                    if (m_SerialPortTool.RTS == 1)
                    {
                        checkBox_RTS.Checked = true;
                    }
                    else if (m_SerialPortTool.RTS == 2)
                    {
                        checkBox_RTS.Checked = false;
                    }
                    else
                    {
                        checkBox_RTS.Checked = false;
                    }

                    if (m_SerialPortTool.DTR == 1)
                    {
                        checkBox_DTR.Checked = true;
                    }
                    else if (m_SerialPortTool.DTR == 2)
                    {
                        checkBox_DTR.Checked = false;
                    }
                    else
                    {
                        checkBox_DTR.Checked = false;
                    }

                    if(m_SerialPortTool.IsRaed16Hex==1)
                    {
                        checkBox_Read16Hex.Checked = true;
                    }else if(m_SerialPortTool.IsRaed16Hex == 2)
                    {
                        checkBox_Read16Hex.Checked = false;
                    }
                    else
                    {
                        checkBox_Read16Hex.Checked = false;
                    }

                    if(m_SerialPortTool.IsSend16Hex==1)
                    {
                        checkBox_Send16Hex.Checked = true;
                    }else if(m_SerialPortTool.IsSend16Hex==2)
                    {
                        checkBox_Send16Hex.Checked = false;
                    }
                    else
                    {
                        checkBox_Send16Hex.Checked = false;
                    }

                    btn_Open.Enabled = false;
                    btn_Close.Enabled = true;
                    tool_Display.Text = "串口打开成功！";
                }
                else
                {
                    btn_Open.Enabled = true;
                    btn_Close.Enabled = false;

                    if (m_SerialPortTool.RTS == 1)
                    {
                        checkBox_RTS.Checked = true;
                    }
                    else if (m_SerialPortTool.RTS == 2)
                    {
                        checkBox_RTS.Checked = false;
                    }
                    else
                    {
                        checkBox_RTS.Checked = false;
                    }

                    if (m_SerialPortTool.DTR == 1)
                    {
                        checkBox_DTR.Checked = true;
                    }
                    else if (m_SerialPortTool.DTR == 2)
                    {
                        checkBox_DTR.Checked = false;
                    }
                    else
                    {
                        checkBox_DTR.Checked = false;
                    }
                }
                tool_Display.Text = "串口模块加载成功";
            }
            catch
            {
                tool_Display.Text = "串口模块加载失败！！！";
            }
          

        }

        
        private void checkBox_RTS_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_RTS.Checked)
            {
                m_SerialPortTool.RTS = 1;
            }
            else
            {
                m_SerialPortTool.RTS = 2;
            }
        }

        private void checkBox_DTR_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_DTR.Checked)
            {
                m_SerialPortTool.DTR = 1;
            }
            else
            {
                m_SerialPortTool.DTR = 2;
            }
        }


        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Open_Click(object sender, EventArgs e)
        {
            try
            {
                m_SerialPortTool.Port = Cmb_Port.Text;  //串口号
                m_SerialPortTool.Baud = Convert.ToInt32(Cmb_Baud.Text);
                m_SerialPortTool.Parity = Cmb_Parity.Text;
                m_SerialPortTool.DataBits = Convert.ToInt32(Cmb_DataBits.Text);
                m_SerialPortTool.StopBits = Cmb_StopBits.Text;
                m_SerialPortTool.OpenSerialPore();
                if (m_SerialPortTool.IsOpenState)
                {
                    tool_Display.Text = "串口打开成功";
                    btn_Open.Enabled = false;
                    btn_Close.Enabled = true;
                }
                else
                {
                    tool_Display.Text = "串口打开失败！！！";
                    btn_Open.Enabled = true;
                    btn_Close.Enabled = false;
                }
            }
            catch
            {
                tool_Display.Text = "串口打开失败！！！";
                btn_Open.Enabled = true;
                btn_Close.Enabled = false;
            }
        }

        /// <summary>
        /// 关闭串口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Close_Click(object sender, EventArgs e)
        {
            m_SerialPortTool.CloseSerialPore();
            if(!m_SerialPortTool.IsOpenState)
            {
                btn_Close.Enabled = false;
                btn_Open.Enabled = true;
                tool_Display.Text = "串口关闭";
                Global.AddLogInfoEvent(LogLevel.Info, "串口关闭");
            }
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Send_Click(object sender, EventArgs e)
        {
            m_SerialPortTool.SendData(txt_Message.Text.Trim());
        }

        /// <summary>
        /// 设置是否16进制读取
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox_Read16Hex_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox_Read16Hex.Checked)
            {
                m_SerialPortTool.IsRaed16Hex = 1;
            }
            else
            {
                m_SerialPortTool.IsRaed16Hex = 2;
            }
        }

        /// <summary>
        /// 设置是否16进制发送
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox_Send16Hex_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox_Send16Hex.Checked)
            {
                m_SerialPortTool.IsSend16Hex = 1;
            }
            else
            {
                m_SerialPortTool.IsSend16Hex = 2;
            }
        }

        /// <summary>
        /// 保存参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                m_SerialPortTool.Port = Cmb_Port.Text;  //串口号
                m_SerialPortTool.Baud = Convert.ToInt32(Cmb_Baud.Text);
                m_SerialPortTool.Parity = Cmb_Parity.Text;
                m_SerialPortTool.DataBits = Convert.ToInt32(Cmb_DataBits.Text);
                m_SerialPortTool.StopBits = Cmb_StopBits.Text;
                m_SerialPortTool.WriteParameter();
                MessageBox.Show("保存成功");
            }
            catch(Exception ex)
            {
                MessageBox.Show("保存失败" + ex.ToString());
            }
         
        }
    }
}
